<?php

$arrjson = Array(
    "湖南省" => Array(
        "val:" => 43,
        "items:" => Array
            (
            "湘西土家族苗族自治州" => Array
                (
                "val:" => 4331,
                "items:" => Array(
                    "吉首市" => Array
                        (
                        "val:" => 433101,
                        "items:" => Array
                            (
                            "双塘镇" => 433101103,
                           "峒河街道" => 433101002,
                        )
                    ),
                   "泸溪县" => Array
                        (
                        "val:" => 433122,
                        "items:" => Array
                            (
                            "潭溪镇" => 433122105,
                            "达岚镇" => 433122102,
                            "兴隆场镇" => 433122104,
                        )
                    )
                )
            ),
            "岳阳市" => Array
                (
                "val:" => 4331,
                "items:" => Array(
                    "吉首市" => Array
                        (
                        "val:" => 433101,
                        "items:" => Array
                            (
                            "双塘镇" => 433101103,
                            "峒河街道" => 433101002,
                        )
                    ),
                    "泸溪县" => Array
                        (
                        "val:" => 433122,
                        "items:" => Array
                            (
                            "潭溪镇" => 433122105,
                            "达岚镇" => 433122102,
                            "兴隆场镇" => 433122104,
                        )
                    )
                )
           ),
        )
    )
);

//echo json_encode($arrjson);

$conn = mysql_connect("localhost", "root", "root");
if (!$conn) {
    echo "连接失败";
    exit();
}
mysql_select_db("yiimovie");
mysql_query("set names 'utf8'");


$name = "湖南省";

$sql = "select * from regionsback where name='$name'";
$res = mysql_query($sql);

$arr = array();
while ($row = mysql_fetch_assoc($res))
{
    $arr[] = $row; 
}

//$str = "";
//
//$char= "val:";
//$items = "items:";
//function get_str($self_id,$result,$str,$level)
//{
//    $char= "val:";
//    $items = "items:";
//    $temp_level= $level+1;
//    $sql = "select * from regionsback where parent_id=$self_id and level=$level";
//    $res = mysql_query($sql);
//    if($res)
//    {   
//        $temp_arr = array();
//        while($row = mysql_fetch_assoc($res))
//        {
//            $self_id = $row["id"];
//            $name = $row["name"];
//            $code = $row["code"];
//            $level = $row["level"];
//            $str ='"'.$name.'"'.':'.'{'.'"'.$char.'"'.':'.'"'.$code.'"'.','.'"'.$items.'"'.':'.'{';
//            $temp_arr[$temp_level]=array($self_id=>$str);
//        }
//        
//    }
    
       
    
    
    
    
    
    
    
        //while($row = mysql_fetch_assoc($res))
        //{
//            $self_id = $row["id"];
//            $name = $row["name"];
//            $code = $row["code"];
//            $level = $row["level"];
//            $str.=$flag?'"'.$name.'"'.':'.'{'.'"'.$char.'"'.':'.'"'.$code.'"'.','.'"'.$items.'"'.':'.'{'
//                    :'"'.$result["name"].'"'.":".'"'.$result["code"].'"'.',';
            
//            if($level!=4)
//            {
//                $str .='"'.$name.'"'.':'.'{'.'"'.$char.'"'.':'.'"'.$code.'"'.','.'"'.$items.'"'.':'.'{';
//            }
//            
//            get_str($self_id,$row,$str);
//            if($level==4)
//            {
//                $str.='"'.$row["name"].'"'.":".'"'.$row["code"].'"'.',';
//            }                
//            $str.='},';
                    
        //}
//    $str.='}';
//    $str = substr($str,0, strlen($str)-1);
//    return $str;
//}

function get_parent($level,$name,$code,$current_id,$parent_id)
{
    $arr = array();
    
    
    //while($level<5)
    {
        switch($level)
        {
            case 1:
                $sql = "select * from regionsback where parent_id=$current_id";
                $result = mysql_query($sql);
                $arr_city = array();
                
                while ($row1 = mysql_fetch_assoc($result))
                {
                    //$arr_city[] = $row1;
                    $city_current_id = $row1["id"];
                    $sql2 = "select * from regionsback where parent_id=$city_current_id";
                    $result2 = mysql_query($sql2);
                    $arr_current_area = array();
                    while($row2 = mysql_fetch_assoc($result2))
                    {
                        //$arr_current_area[] = $row2;
                        $area_current_id = $row2["id"];
                        $arr_twons = array();
                        $sql3 = "select * from regionsback where parent_id=$area_current_id";
                        $result3 = mysql_query($sql3);
                        while($row3 = mysql_fetch_assoc($result3))
                        {
                            $arr_twons[$row3["name"]] = $row3["code"];
                        }
                        $arr_current_area[$row2["name"]]=array("val:"=>$row2["code"],"items:"=>$arr_twons);
                    }
                    $arr_city[$row1["name"]]=array("val:"=>$row1["code"],"items:"=>$arr_current_area);
                }
                $arr[$name]=array("val:"=>$code,"items:"=>$arr_city);
                
                
                break;
            case 2:
                
                break;
            case 3:
                $sql = "select * from regionsback where parent_id=$current_id";
                $result = mysql_query($sql);
                $arrtemp = array();
                if($result)
                {
                    while($row = mysql_fetch_assoc($result))
                    {
                        $arrtemp[$result["name"]]=$result["code"];
                    }
                    $arr[$name]=array("val:"=>$code,"items:"=>$arrtemp);
                }
                break;
            case 4:
                $sql = "select * from regionsback where id=$parent_id";
                $result = mysql_query($sql);
                $arrtemp = array();
                if($result)
                {
                    $row = mysql_fetch_assoc($result);
                    $arrtemp[$name]=$code;
                    $arr[$row["name"]]=array("val:"=>$row["code"],"items:"=>$arrtemp);
                }
                break;
            default :
                break;
        }
    }
    return $arr;
}




function get_sun($level,$name,$current_id)
{
    
}

$strarr = array();

foreach ($arr as $val)
{
    $name = $val["name"];
    $code = $val["code"];
    $level = $val["level"];
    $current_id = $val["id"];
    $parent_id = $val["parent_id"];
    $strarr[] = get_parent($level, $name, $code, $current_id, $parent_id);
}

foreach ($strarr as $v)
{
    $json = json_encode($v);
    echo $json;
}

?>
